package com.frank.towerrush.split;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

/**
 * 怪物
 * 
 * @author Frank
 *
 */
public class Monster extends Location {
    int maxHp = 1000;
    int hp = 1000;

    /**
     * 怪物的图片 每一行代表朝四个方向行走 每一列代表行走的一帧
     */
    Bitmap[][] images;

    /**
     * 怪物的类型ID 也是图片的ID
     */
    int typeID;

    /**
     * 移动路径
     */
    public static Path path;

    /**
     * 怪物所在路段的索引
     */
    private int whichWay = 0;

    /**
     * 移动速度
     */
    private int speed = 10;

    /**
     * 绘图索引
     */
    private int freamIndex = -1;

    public Monster(Context context, int typeID) {
        super(path.getStart());

        this.typeID = typeID;

        Bitmap source = BitmapFactory.decodeResource(context.getResources(), typeID);
        init(source);
    }

    /**
     * 初始化怪物类
     * 
     * @param source
     *            怪物的全部图片
     */
    private void init(Bitmap source) {
        // 分割图片储存到images中
        images = new Bitmap[4][];

        int height = source.getHeight() / 4;
        int width = source.getWidth() / 3;

        for (int i = 0; i < images.length; i++) {
            images[i] = new Bitmap[3];
            for (int j = 0; j < images[i].length; j++) {
                images[i][j] = Bitmap.createBitmap(source, j * width, i * height, width, height);
            }
        }
        setSize(height, width);
    }

    /**
     * 处理怪物的移动
     */
    private void move() {
        // 当前位置
        Flag now = getLoc();
        if (path.moveToNext(now, speed, whichWay)) {
            whichWay++;
        }
        setLoc(now);
    }

    /**
     * 绘制
     */
    public void paint(Canvas canvas, Paint paint) {
        if (!isEnd()) {
            if (hp != maxHp) {
                drawHPLine(canvas, paint);
            }
            canvas.drawBitmap(images[path.getRoad(whichWay).getDir()][frameIndex()], null, getRectF(), paint);
            move();
        } else {
            whichWay = 0;
            setLoc(path.getStart());
        }
    }

    public void drawHPLine(Canvas canvas, Paint paint) {
        paint.setColor(Color.RED);
        canvas.drawLine(getRectF().left, getRectF().top - getRectF().width() / 2, getRectF().right,
                getRectF().top - getRectF().width() / 2, paint);
        paint.setColor(Color.GREEN);
        canvas.drawLine(getRectF().left, getRectF().top - getRectF().width() / 2,
                getRectF().left + getRectF().width() * hp / maxHp, getRectF().top - getRectF().width() / 2, paint);
    }

    /**
     * 设置路径
     * 
     * @param flags
     */
    public static void setPath(List<Flag> flags) {
        path = new Path(flags);
    }

    /**
     * 刷新绘图索引并返回
     * 
     * @return 新的绘图索引
     */
    private int frameIndex() {
        freamIndex++;
        if (freamIndex == 3) {
            freamIndex = 0;
        }
        return freamIndex;
    }

    /**
     * 判断走到了终点
     * 
     * @return 结果
     */
    public boolean isEnd() {
        return whichWay >= path.roadCount();
    }

    /**
     * 受到攻击
     */
    public void hit(int dmg) {
        hp -= dmg;
    }

    public boolean isDead() {
        return hp <= 0;
    }
}
